package com.se7en.ufutil;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class FloorPlan extends BalloonItemizedOverlay<OverlayItem> { 

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private FloorModel[] classLocs;
	DataAccess dataAccess;
	MapView mapView;
	boolean what;
	String theRoom;
	
	public FloorPlan(Drawable defaultMarker, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
	}

	public FloorPlan(Drawable defaultMarker, Context context, DataAccess dataAccess, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		//Paul: initialization stuffs
		mContext = context;
		this.dataAccess = dataAccess;
		this.mapView = mapView;
		classLocs = dataAccess.getAllClasses();
		OverlayItem temp;
		Drawable icon = context.getResources().getDrawable(R.drawable.classroom_marker);
		
		
		//Paul: adding all the classlocations to the list of overlay items
		if(classLocs.length > 1) {
			for (FloorModel floor : classLocs)
			{
				//Paul: initialization of myoverlayitem
				temp = new OverlayItem(floor.location, floor.name, "Click here to open the floor plan.");
				temp.setMarker(icon);
				icon.setBounds(-icon.getIntrinsicWidth()/2, -icon.getIntrinsicHeight(), icon.getIntrinsicWidth() /2, 0);
				this.addOverlay(temp);
			}
		}
	}
	

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	public void search(String bld, String room) {
		int[] loc=DataAccess.getRoomLocation(room, bld);
    	if (loc[0]==-1) {
    		//room not found
    		Toast.makeText(mapView.getContext() , "Room not found",
    				Toast.LENGTH_LONG).show();
    		//((EditText) searchBar).selectAll();
    		return;
    	}
    	else {
    		//room found
    		//Toast.makeText(mapView.getContext() , "Room was found",
    		//		Toast.LENGTH_SHORT).show();
    		int itemNum = 0;
    		if(classLocs.length > 1) {
    			for (FloorModel floor : classLocs)
    			{
    				if (floor.name.equals(bld)) {
    					itemNum = floor.fID;
    					mySnip = "Click here to open the floor plan to room " + room;
    					theRoom = room;
    					customsnippet = true;
    					onTap(floor.location, mapView);
    					
    				}
    			}
    		}
    		
    		
    	}
	}
	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		//MyOverlayItem item = mOverlays.get(index);
		//Toast dialog = Toast.makeText(mContext, item.getTitle(), 5000);
		//dialog.show();
		Intent i = new Intent(mContext, FloorDisplay.class);
		i.putExtra("name", item.getTitle());
		if (what) {
			i.putExtra("room", theRoom);
		}
		//i.putExtra("floors", classLocs[Integer.parseInt(item.getSnippet())].floors);
		mContext.startActivity(i);

		return true;
	}
	
	/**
	 * Creates and displays the balloon overlay by recycling the current 
	 * balloon or by inflating it from xml. 
	 * @return true if the balloon was recycled false otherwise 
	 */
	@Override
	protected boolean createAndDisplayBalloonOverlay(){
		if(customsnippet) {
			what = true;
		} else {
			what = false;
		}
		boolean isRecycled;
		if (balloonView == null) {
			balloonView = createBalloonOverlayView();
			clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
			clickRegion.setOnTouchListener(createBalloonTouchListener());
			isRecycled = false;
		} else {
			isRecycled = true;
		}
	
		balloonView.setVisibility(View.GONE);
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		if (mapOverlays.size() > 1) {
			hideOtherBalloons(mapOverlays);
		}
		
		if (currentFocussedItem != null) {
			if (customsnippet && mySnip != null) {
				balloonView.setcustomData(currentFocussedItem,mySnip);
			} else {
				balloonView.setData(currentFocussedItem);
			}
		}
		
		GeoPoint point = currentFocussedItem.getPoint();
		MapView.LayoutParams params = new MapView.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
				MapView.LayoutParams.BOTTOM_CENTER);
		params.mode = MapView.LayoutParams.MODE_MAP;
		
		balloonView.setVisibility(View.VISIBLE);
		
		if (isRecycled) {
			balloonView.setLayoutParams(params);
		} else {
			mapView.addView(balloonView, params);
		}
		customsnippet = false;
		return isRecycled;
	}

}
